<template>
  <div class="article-list">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(article, index) in articles"
        :key="index"
        :title="article.title"
      />
    </van-list>
  </div>
</template>

<script>
//导入获取文章列表的请求
import { getArticles } from "@/api/article";
export default {
  name: "ArticleList",
  components: {},
  props: {
    channel: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      //文章列表
      articles: [],
      //控制加载中的 loading 状态
      loading: false,
      //控制加载结束的状态,当加载结束，不再触发加载更多
      finished: false,
      //获取下一页数据的时间戳
      timestamp: null,
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    //请求加载数据
    async onLoad() {
      //1.请求获取数据
      const { data } = await getArticles({
        //频道id
        channel_id: this.channel.id,
        //时间戳 请求新的数据传当前的时间戳,请求历史数据传指定的时间戳
        timestamp: this.timestamp || Date.now(),
        //是否包含置顶,进入页面第一次请求时要包含置顶文章,1:包含,0:不包含
        with_top: 1,
      });
      //2.把数据放到articles文章列表中
      console.log(data);
      const { results } = data.data;
      this.articles.push(...results);
      //3.设置本次加载状态结束
      this.loading = false;
      //4.数据全部加载完成
      if (results.length) {
        //数据还没有加载完,更新获取下一页数据的页码
        this.timestamp = data.data.pre_timestamp;
      } else {
        //没有数据了,将加载状态设置结束
        this.finished = true;
      }
    },
  },
};
</script>

<style scoped lang="less">
.article-list {
  position: fixed;
  left: 0;
  right: 0;
  top: 90px;
  bottom: 50px;
  overflow-y: auto;
}
</style>